package com.atguigu.bigdata.chapter11.function;

import com.atguigu.bigdata.bean.WaterSensor;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.functions.TableFunction;

import static org.apache.flink.table.api.Expressions.$;

/**
 * @Author lzc
 * @Date 2022/9/9 14:09
 */
public class Flink02_Function_Table_2 {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        
        DataStreamSource<WaterSensor> stream = env.fromElements(
            new WaterSensor("hello hello atguigu", 1000L, 10),
            new WaterSensor("hello hello", 2000L, 20),
            new WaterSensor("atguigu world", 3000L, 30),
            new WaterSensor("abc a", 4000L, 40),
            new WaterSensor("aa cc dd ee ff gg", 8000L, 80)
        );
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        
        Table table = tEnv.fromDataStream(stream, $("id"), $("ts"), $("vc"));
        tEnv.createTemporaryView("sensor", table);
        
        // 使用自定义函数有两种用法:
        // 1. 在table_api中使用
        // 1.1 内联用法
        
        // 1.2 先注册后使用
        tEnv.createTemporaryFunction("my_split", MySplit.class);
        /*table
            // 内连接
//            .joinLateral(call("my_split", $("id")))
            .leftOuterJoinLateral(call("my_split", $("id")))
            .select($("id"), $("word"), $("len"))
            .execute()
            .print();*/
        // 2. 在sql中使用, 必须先注册函数
        //内连接1: select  .. from a join b on a.id=b.id
        //内连接2: select  .. from a, b where a.id=b.id
        /*tEnv.sqlQuery("select " +
                          " id, word, len " +
                          "from sensor " +
                          "join lateral table(my_split(id)) on true")
            .execute()
            .print();*/
        /*tEnv.sqlQuery("select " +
                          " id, word, len " +
                          "from sensor " +
                          ", lateral table(my_split(id))")
            .execute()
            .print();*/
    
        tEnv.sqlQuery("select " +
                          " id, word, len " +
                          "from sensor " +
                          ", lateral table(my_split(id))")// t是临时表的表名 a和b是按照顺序来的列名
            .execute()
            .print();
        
        /*tEnv.sqlQuery("select " +
                          " id, word, len " +
                          "from sensor " +
                          "left join lateral table(my_split(id)) on true")
            .execute()
            .print();*/
        
        
        
    }
    
    // 如果这里的泛型使用的是POJO形式, 则不需要添加注解来说明每列的类型
    public static class MySplit extends TableFunction<WordLen> {
        // 约定方法
        // 返回值必须是void
        // 方法名必须是 eval
        // 参数根据实际情况来定
        public void eval(String s){
            
            
            
            if (s != null) {
    
                if (s.contains("aa")) {
                    return;
                }
    
                for (String word : s.split(" ")) {
                    // 这个方法执行几次, 结果就是几行
                    // 怎么告诉flink 每行有两列
                    collect(new WordLen(word, word.length()));
                }
                
            }
            
        }
    }
    
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class WordLen{
        private String word;
        private Integer len;
    }
    
}
/*
传入一个字符串, 对字符串按照空格切割, 返回切割后的每个单词和这个单词的长度

"hello hello atguigu"
        hello   5
        hello   5
        atguigu 7
        
"hello atguigu"
        hello   5
        atguigu 7
        
如何让原表和制出来的表连在一起?
    join
 */
